<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;

use app\common\Common;

use think\Request;
use think\Db;

class Role extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = 'role';
        $defaultWhere = $this->getDefaultWhere();
        $this->defaultWhere = $defaultWhere;
        $model = model('menu');
        $role =$model->where(['status'=>1,'parent_id'=>0])->select();
        $rolesun =$model->where(['status'=>1])->select();
        $this->assign('role',$role);
        $this->assign('rolesun',$rolesun);
    }

    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        // Reset filter
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = model($this->model);

            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'] + 1;
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }
            $data = $model->where($where)->limit($start,$length)->order($order)->select();
            $count = $model->count();
            $result = [
                'code' => '0',
                'msg' => '请求成功',
                'data' => $data,
                'count' => $count,
            ];

            return json($result);
        }

        return $this->fetch('list');

    }

    /**
     * Add
     */
    public function add(Request $request)
    {

        return parent::add($request);
    }

    /**
     * Add Post
     */
    public function addPost(Request $request, $redirect = '')
    {
        $param = $request->param();
        $model = model($this->model);
        if ($request->isAjax()){
            if (empty($param['role_name'])){
                return $this->error('角色名字为空');
            }
            if (empty($param['role_auth'])){
                return $this->error('权限为空');
            }
            $param['role_auth'] = implode(',',$param['role_auth']);
            $result = $model->save($param);
            if (!$result){
                return $this->error('添加失败');
            }
            $logInfo = $this->admin->name . '添加了一条' . lang($this->model) . '数据。';


            // 写入日志
            Common::adminLog($request, $logInfo);
            return $this->success('添加成功！', 'admin/setting/index');
        }
    }

    /**
     * Edit
     */
    public function edit(Request $request){

        $model = db($this->model);

        $id = $request->param('id');

        // Get data info
        $info = $model->where(['id'=>$id])->find();
        if(!$info){
            return $this->error('error !', 'admin/' . strtolower($this->model) . '/index');
        }
        $info['role_auth'] = explode(',',$info['role_auth']);
        $this->assign('info',$info);
        //dump($info['role_auth']);
        return view();
    }

    /**
     * Edit Post
     */
    public function editPost(Request $request, $redirect = ''){
        $param = $request->param();
        $model = model($this->model);



        if ($request->isAjax()){
            if (empty($param['role_name'])){
                return $this->error('角色名字为空');
            }
            if (empty($param['role_auth'])){
                return $this->error('权限为空');
            }
            $param['role_auth'] = implode(',',$param['role_auth']);
            $data = [
                'role_name' =>$param['role_name'],
                'role_auth' =>$param['role_auth'],
            ];
            $result = $model->update($data,['id'=>$param['id']],true);

            if (!$result){

                return $this->error('修改失败');
            }
            $logInfo = $this->admin->name . '修改了一条' . lang($this->model) . '数据。';


            // 写入日志
            Common::adminLog($request, $logInfo);
            return $this->success('修改成功！', 'admin/setting/index');
        }
    }

    /**
     * 启用/禁用
     */
    public function handle(Request $request){
        $model = model($this->model);
        if ($request->isPost()) {
            $data = $request->param();
            $result = $model->allowField(true)->save($data, ['id' => $data['id']]);
            if($result !== false) {
                return $this->success('操作成功！', 'admin/' . strtolower($this->model) . '/index');
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('提交方式错误！');
        }
    }
    //权限列表
    public function role(Request $request)
    {
        return view();
    }
}